#ifndef __GAME_SERVER_HPP__
#define __GAME_SERVER_HPP__

#include "user.hpp"
#include "room.hpp"
#include "fastdo.hpp"
#include <vector>
#include <typeindex>
#include "http.hpp"
#include "ws_websocket.hpp"

class GameServer;
class Protocol;
typedef winux::SharedPointer<ws::WsHttpRequestCtx> pWsReqCtx;
typedef void (*ActionType)(Protocol *, pWsReqCtx, GameServer &);
class GameServer
{
private:
    int roomIdCounter = 0;
	Mutex __mtxForUserList;
	Mutex __mtxForRoomList;

public:
    vector<winux::SharedPointer<User>> userList;
    vector<winux::SharedPointer<Room>> roomList;

public:
    GameServer();
    void runAction(Protocol *msg, pWsReqCtx ctx);
    void run();

    public:
    User* newUser();

public:
    Room* newRoom();
    Room* getRoom( int id );
    Room* getRoomByUser( User* user );
    User* getUser(pWsReqCtx ctx);
protected:
    void _onOpen(pWsReqCtx requestCtxPtr, http::Url const &url);
    void _onMessage(pWsReqCtx requestCtxPtr, http::Url const &url, winux::AnsiString const &message, int messageType);
    void _onClose(pWsReqCtx requestCtxPtr, http::Url const &url, winux::uint16 errCode, winux::AnsiString const &errStr);
    void _onError(pWsReqCtx requestCtxPtr, http::Url const &url, ws::ErrorCode errCode);
    void _userDisconnect(User *user);
};

#endif //__GAME_SERVER_HPP__